﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aerthe.Model.Tasks
{
    public class MTask1
    {
        public List<ListsItem> ListTask = new List<ListsItem> { };
        private string BuforString;

        public MTask1()
        {
            using (var Base = new NorthwindEntities())
            {
                var ListEmployees = Base.Employees.Select(m => new
                {
                    InfoEmployee = m,
                    ValueDisposal = m.Orders.Sum(n => n.Order_Details.Sum(o => o.Quantity * o.UnitPrice)),
                    ListTerritories = m.Territories.Select(n => n.TerritoryDescription).Distinct()
                });

                foreach (var a in ListEmployees)
                {
                    foreach (var b in a.ListTerritories)
                    {
                        this.ListTask.Add(new ListsItem(
                                a.InfoEmployee.EmployeeID.ToString().Trim(),
                                a.InfoEmployee.FirstName.Trim(),
                                a.ValueDisposal.ToString().Trim(),
                                b.Trim()
                            ));
                    }
                }
            }
        }

        public void MViewConsole()
        {
            Console.WriteLine("| {0,2} | {1,10} | {2,13:C} | {3}", "ID", "Last name", "Turnover", "Cities");

            foreach (var a in this.ListTask)
            {
                if (this.BuforString == a.Item1)
                {
                    Console.Write(", {0}", a.Item4);
                }
                else
                {
                    this.BuforString = a.Item1;

                    Console.Write("\n| {0,2} | {1,10} | {2,13:C} | {3}", a.Item1, a.Item2, a.Item3, a.Item4);
                }
            }

            Console.WriteLine("");
        }
    }
}
